//只显示后六位字符 传入replaceStr参数表示用这个传入的字符代替被隐藏的部分
function hideStr (value:string, replaceStr:string = '') {
    if(!value) return ''
    let bankNumArr = value.split('')
    bankNumArr = bankNumArr.slice(-6, bankNumArr.length); //后六位数字
    bankNumArr = [replaceStr,...bankNumArr]
    return bankNumArr.join('');
};
//只显示前 n 位文字, 默认8位, 省略部分用传入的字符代替
function ellipsis (value:string, replaceStr:string = '', n:number = 8){
    if(!value) return ''
    if(value.length <= n) replaceStr = ''
    let beforeStr = value.split('')
    beforeStr = beforeStr.slice(0, n)
    beforeStr = [...beforeStr, replaceStr]
    return beforeStr.join('')
}
//时间格式化
function toTimeFormat (t: number, f: string = 'yyyy-MM-dd') {
    if (t) {
      const newDate = new Date(t)
      const date: IDate = {
        'M+': newDate.getMonth() + 1,
        'd+': newDate.getDate(),
        'h+': newDate.getHours(),
        'm+': newDate.getMinutes(),
        's+': newDate.getSeconds(),
        'q+': Math.floor((newDate.getMonth() + 3) / 3),
        'S+': newDate.getMilliseconds()
      }
      if (/(y+)/i.test(f)) {
        f = f.replace(RegExp.$1,
          (newDate.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      for (const k in date) {
        if (new RegExp('(' + k + ')').test(f)) {
          f = f.replace(RegExp.$1,
            RegExp.$1.length === 1 ? date[k].toString() : ('00' + date[k]).substr(
              ('' + date[k]).length).toString())
        }
      }
      return f
    } else {
      return ''
    }
}
//
function dateAdd(interval:string, number:number, date) {
  switch (interval) {
    case "y": {
      date.setFullYear(date.getFullYear() + number);
      return date;
      break;
    }
    case "q": {
      date.setMonth(date.getMonth() + number * 3);
      return date;
      break;
    }
    case "m": {
      date.setMonth(date.getMonth() + number);
      return date;
      break;
    }
    case "w": {
      date.setDate(date.getDate() + number * 7);
      return date;
      break;
    }
    case "d": {
      date.setDate(date.getDate() + number);
      return date;
      break;
    }
    case "h": {
      date.setHours(date.getHours() + number);
      return date;
      break;
    }
    case "m": {
      date.setMinutes(date.getMinutes() + number);
      return date;
      break;
    }
    case "s": {
      date.setSeconds(date.getSeconds() + number);
      return date;
      break;
    }
    default: {
      date.setDate(d.getDate() + number);
      return date;
      break;
    }
  }
}
// 速贴注册贷款码验证函数
function checkLoanCardID(loanCardCode: string) {
    var financecode = new Array();
    if (!loanCardCode || loanCardCode == '' || loanCardCode == null) {
        return false
    }
    for (var i = 0; i < loanCardCode.length; i++) {
        financecode[i] = loanCardCode.charCodeAt(i);
    }
    //贷款卡卡号长度判 断 modify by ddbai 2016年4月22日 13:35:51
    if (loanCardCode.length > 16) return false;
    var weightValue = new Array(14);
    var checkValue = new Array(14);
    var totalValue = 0;
    var c = 0;
    weightValue[0] = 1;
    weightValue[1] = 3;
    weightValue[2] = 5;
    weightValue[3] = 7;
    weightValue[4] = 11;
    weightValue[5] = 2;
    weightValue[6] = 13;
    weightValue[7] = 1;
    weightValue[8] = 1;
    weightValue[9] = 17;
    weightValue[10] = 19;
    weightValue[11] = 97;
    weightValue[12] = 23;
    weightValue[13] = 29;
    for (var j = 0; j < 14; j++) {
        if (financecode[j] >= 65 && financecode[j] <= 90) {
            checkValue[j] = (financecode[j] - 65) + 10;
        } else if (financecode[j] >= 48 && financecode[j] <= 57) {
            checkValue[j] = financecode[j] - 48;
        } else {
            return false;
        }
        totalValue += weightValue[j] * checkValue[j];
    }
    c = 1 + totalValue % 97;
    var val = (financecode[14] - 48) * 10 + (financecode[15] - 48);
    return val == c;
}
// 速贴统一社会信用代码验证
function checkCrditCode(orgCode: string) {
    if (orgCode.length != 18) {
        return "统一社会信用代码长度必须为18位.";
    }

    //字符集数组
    var fin_array = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T', 'U', 'W', 'X', 'Y'];
    //加权数组
    var i_array = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];
    //结果数组
    var val_array = [];
    //相乘数组
    var ch_array = [];
    //Sigma
    var sigma = 0;
    var finIdex =0;
    //部分IE浏览器不支持数组中的indexOf函数, 故加上此代码;
    if (!Array.indexOf) {
        Array.prototype.indexOf = function (obj) {
            for (var i = 0; i < this.length; i++) {
                if (this[i] == obj) {
                    return i;
                }
            }
            return -1;
        }
    }
    //转换成字符集的值
    for (var i = 0; i < 17; i++) {
        val_array[i] = fin_array.indexOf(orgCode.charAt(i));
    }
    //校验是否包含字符集之外的字符
    for (var i = 0; i < 17; i++) {
        if (val_array[i] == -1) {
            return "统一社会信用代码包含非法字符";
        }
    }
    //生成加权结果
    for (var i = 0; i < 17; i++) {
        ch_array[i] = val_array[i] * i_array[i];
    }
    //求出Sigma
    for (var i = 0; i < 17; i++) {
        sigma = sigma + ch_array[i];
    }

    if(sigma % 31!=0){
        finIdex = 31 - sigma % 31;
    }

    //求出校验位
    var vali = fin_array[finIdex];

    //查看校验码是否正确
    if (orgCode.charAt(17) == vali) {
        return "正确";
    } else {
        return "统一社会信用代码有误！";
    }
}
// 速贴身份证验证
function CheckLicense(ID: string) {
    var checkedValue = ID;
    checkedValue = checkedValue.trim();
    if (checkedValue.length != 15 && checkedValue.length != 18)
        return false;
    var dateValue;
    if (checkedValue.length == 15)
        dateValue = "19" + checkedValue.substring(6, 12);
    else
        dateValue = checkedValue.substring(6, 14);
    if (!checkDate(dateValue))
        return false;
    if (checkedValue.length == 18)
        return checkPersonId(checkedValue);
    return true;
}
// 汇票线平台身份证验证（允许港澳台身份证）
function checkIDHPX(idCart: string) {
    let checkVal: string = idCart;
    // checkVal = checkVal.trim();
    if(checkVal.length == 15 || checkVal.length == 18){
        // 大陆
        if(!/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/.test(checkVal)){
            return false;
        }else{
            return true;
        }
    }else if(checkVal.length == 8){
        // 香港、澳门
        if(!/^((\s?[A-Za-z])|([A-Za-z]{2}))\d{6}(\([0−9aA]\)|[0-9aA])$/.test(checkVal) && !/^[1|5|7][0-9]{6}\([0-9Aa]\)/.test(checkVal) && !/^[0-9]/.test(checkVal)){
            return false;
        }else {
            return true
        }
    }else if(checkVal.length == 10){
        // 台湾
        if(!/^[a-zA-Z][0-9]{9}$/.test(checkVal)){
            return false;
        }else{
            return true;
        }
    }else {
        return false;
    }
}
// 票据涂抹后的路径处理
function getEndorsementFileName1(imgUrl: string) {
    if (imgUrl == '') {
        return;
    } else {
        var suffix = imgUrl.substr(imgUrl.lastIndexOf('.'), imgUrl.length);
        return imgUrl.replace(suffix, `-1${suffix}`)
    }

}
export {
    hideStr,
    toTimeFormat,
    ellipsis,
    dateAdd,
    checkLoanCardID,
    checkCrditCode,
    CheckLicense,
    checkIDHPX,
    getEndorsementFileName1
}